package com.assetmanage.dao.impl;

import com.assetmanage.dao.DepartmentDao;
import com.assetmanage.model.Department;
import com.assetmanage.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDao {
    @Override
    public List<Department> getAllDepartments() {
        List<Department> departments = new ArrayList<>();
        String sql = "SELECT * FROM departments";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Department department = new Department();
                department.setDepId(rs.getString("dep_id"));
                department.setDepName(rs.getString("dep_name"));
                departments.add(department);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return departments;
    }

    @Override
    public Department getDepartmentById(String depId) {
        String sql = "SELECT * FROM departments WHERE dep_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, depId);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Department department = new Department();
                department.setDepId(rs.getString("dep_id"));
                department.setDepName(rs.getString("dep_name"));
                return department;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return null;
    }

    @Override
    public boolean addDepartment(Department department) {
        String sql = "INSERT INTO departments (dep_id, dep_name) VALUES (?, ?)";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, department.getDepId());
            stmt.setString(2, department.getDepName());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean updateDepartment(Department department) {
        String sql = "UPDATE departments SET dep_name = ? WHERE dep_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, department.getDepName());
            stmt.setString(2, department.getDepId());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean deleteDepartment(String depId) {
        String sql = "DELETE FROM departments WHERE dep_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, depId);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }
} 